﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WJ.Infrastructure.Console
{
    public partial class Form1 : Form
    {
        SynchronizationContext SyncContext = null;
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4001);

        public Form1()
        {
            InitializeComponent();
            SyncContext = SynchronizationContext.Current;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread listen = new Thread(receive);
            listen.IsBackground = true;
            listen.Start();
        }

        private void receive()
        {
            UdpClient uc = new UdpClient(endpoint);
            while (true)
            {
                IPEndPoint ip = null;
                byte[] message = uc.Receive(ref ip);
                string messagestring = Encoding.UTF8.GetString(message);
                SyncContext.Post(change, messagestring);
            }
            
            
        }

        private void change(object str)
        {
            richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\t" + str + "\n\n");
            richTextBox1.Focus();
        }
    }
}
